extends Node
func _ready():
	pass
func _init():
	call_deferred("dockInit")
func _connect():
	pass
var utils = null
var azurCtrl = null
var difficult
var base = null
var window = null
var selWindow = null
func dockInit():
	if globalData.infoDs.has("g_azurlaneBase"):
		base = globalData.infoDs["g_azurlaneBase"]
	if base != null:
		#初始化
		utils = globalData.infoDs["g_azurlaneUtils"]
		azurCtrl = globalData.infoDs["g_azurlane"]
		azurCtrl.connect("resetData", self, "resetData")

var br1 = 25
var br2 = 25
var br3 = 25
var br4 = 25

var bindex1 = 0
var bindex2 = 0
var bindex3 = 0
var bindex4 = 0

func resetData():
	window = null
	br1 = 25
	br2 = 25
	br3 = 25
	br4 = 25
	bindex1 = 0
	bindex2 = 0
	bindex3 = 0
	bindex4 = 0
	selWindow = null

func open():
	if not is_instance_valid(window) or not window is WindowDialog:
		create()

	refreshResource()
	window.popup_centered()
var repeatEdit = null
func create():
	window = utils.createWindowDialog(sys.main.get_node("ui"), "港区船坞", Vector2(800, 450))	
	var headerBox = utils.createHBox(window)
	headerBox.name = "header"
	headerBox.rect_position = Vector2(30, 30)

	headerBox.add_child(utils.createTextureRect("icons/mofang.png"))
	var mfLb = utils.createRichTextLabel("[color=#DC143C]" + str(int(azurCtrl.mf)), headerBox, Vector2(100, 40))
	mfLb.name = "mfLb"

	utils.createRichTextLabel("[color=#BDB76B]重复次数：", headerBox, Vector2(110, 40))
	repeatEdit = utils.createLineEdit(headerBox, Vector2(100, 40))
	repeatEdit.text = "1"

	utils.createRichTextLabel("[color=#BDB76B]选择建造项目：", window, Vector2(400, 40), Vector2(30, 80))

	var grid = utils.createGridContainer(window, 4)
	grid.rect_position = Vector2(30, 120)

	utils.createItemButton(grid, self, "轻型舰", "build", [1])
	utils.createRichTextLabel("[color=#BDB76B]消耗：80魔方", grid, Vector2(240, 40))

	utils.createItemButton(grid, self, "中型舰", "build", [2])
	utils.createRichTextLabel("[color=#BDB76B]消耗：80魔方", grid, Vector2(240, 40))

	utils.createItemButton(grid, self, "重型舰", "build", [3])
	utils.createRichTextLabel("[color=#BDB76B]消耗：80魔方", grid, Vector2(240, 40))

	utils.createItemButton(grid, self, "特型舰", "build", [4])
	utils.createRichTextLabel("[color=#BDB76B]消耗：80魔方", grid, Vector2(240, 40))

	utils.createRichTextLabel("[color=#BDB76B]产出说明：\n[color=#C0C0C0]轻型(驱逐/辅助/3布里) 中型(轻巡/重巡/3布里) \n重型(战列/3布里) 特型(轻航/航母/3布里)\n每种类型的建造每连续建造5次，下次建造改为随机4选1", window, Vector2(700, 180), Vector2(30, 250))

func refreshResource():
	window.get_node("header/mfLb").bbcode_text = "[color=#DC143C]" + str(int(azurCtrl.mf))

var cost1 = 80
var cost2 = 80
var cost3 = 80
var cost4 = 80

func build(type):
	var repeat = utils.str2num(repeatEdit.text)
	var cost = get("cost%d"%type)
	if cost*repeat > azurCtrl.mf:
		sys.newBaseMsg("警告", "建造失败：魔方不足！")
		return
	var chas = []

	var index = get("bindex%d"%type)
	if index >= 5:
		baodi(type)
		set("bindex%d"%type, 0)
		return

	for i in range(repeat):
		var cha = call("doBuild%d"%type)
		if cha != null:
			index += 1
			set("bindex%d"%type, index)
			if cha is chaData.Info:
				chas.append(cha.id)
			else:
				chas.append(cha)
			azurCtrl.changeMF(-cost)
	if not chas.empty():
		var xx = sys.newMsg("jiangLiMsg")
		xx.get_node("Panel/Label").text = "建造成果"
		xx.get_node("Panel/Button2").hide()
		var context = utils.createScrollContainer(xx.get_node("Panel"), Vector2(1050, 400), Vector2(30, 100))
		var grid = utils.createGridContainer(context, 7, Vector2(150, 75))
		for i in chas:
			if i.find("buli") > -1:
				for j in range(int(i.split("buli")[1])):
					grid.add_child(utils.createTextureRect("icons/buli.png"))
					azurCtrl.changeBuli(1)
			else:
				utils.newChaBt(grid, i)
				sys.main.player.addCha(utils.newChara(i))
		xx.popup()

	refreshResource()

#保底建造
func baodi(type):
	var chas = []
	for i in range(4):
		var cha = call("doBuild%d"%type)
		while cha == null or not cha is chaData.Info or chas.has(cha.id):
			cha = call("doBuild%d"%type)
		chas.append(cha.id)
	if not is_instance_valid(selWindow) or not selWindow is WindowDialog:
		selWindow = utils.createWindowDialog(sys.main.get_node("ui"), "定向建造", Vector2(700, 350))
		selWindow.popup_exclusive = true
		selWindow.get_close_button().hide()
		var grid = utils.createGridContainer(selWindow, chas.size(), Vector2(50, 50))
		grid.name = "grid"

	var grid = selWindow.get_node("grid")
	for i in chas:
		utils.newChaBt(grid, i)
	for i in chas:
		utils.createItemButton(grid, self, "选择", "doChoiceChara", [i, type, grid])
	selWindow.popup_centered()	

func doChoiceChara(id, type, grid):
	var cost = get("cost%d"%type)
	azurCtrl.changeMF(-cost)
	sys.main.player.addCha(utils.newChara(id))
	selWindow.hide()
	refreshResource()
	for i in grid.get_children():
		i.queue_free()

func doBuild1():
	var rnd = sys.rndRan(0, 100)

	if azurCtrl.upChaName != null and (azurCtrl.upChaName.find("驱逐") > -1 or azurCtrl.upChaName.find("辅助") > -1) and not azurCtrl.hadUp:
		if rnd <= 18:
			azurCtrl.hadUp = true
			return azurCtrl.upCha

	if rnd <= br1:
		br1 = 5
		return "buli3"
	br1 = min(70, br1 + 20)
	
	if sys.rndPer(65):
		#驱逐舰
		return sys.rndListItem(chaData.shipTypeDic[sys.CharaClass.DD])
	else:
		#辅助
		return sys.rndListItem(chaData.shipTypeDic[sys.CharaClass.SUP])

func doBuild2():
	var rnd = sys.rndRan(0, 100)

	if azurCtrl.upChaName != null and (azurCtrl.upChaName.find("轻巡") > -1 or azurCtrl.upChaName.find("重巡") > -1) and not azurCtrl.hadUp:
		if rnd < 24:
			azurCtrl.hadUp = true
			return azurCtrl.upCha

	if rnd <= br2:
		br2 = 5
		return "buli3"
	br2 = min(70, br2 + 20)

	if sys.rndPer(55):
		#轻巡
		return sys.rndListItem(chaData.shipTypeDic[sys.CharaClass.CL])
	else:
		#重巡
		return sys.rndListItem(chaData.shipTypeDic[sys.CharaClass.CA])

func doBuild3():
	var rnd = sys.rndRan(0, 100)

	if azurCtrl.upChaName != null and (azurCtrl.upChaName.find("战列") > -1) and not azurCtrl.hadUp:
		if rnd < 30:
			azurCtrl.hadUp = true
			return azurCtrl.upCha

	if rnd <= br3:
		br3 = 5
		return "buli3"
	br3 = min(70, br3 + 20)

	#战列
	return sys.rndListItem(chaData.shipTypeDic[sys.CharaClass.BB])

func doBuild4():
	var rnd = sys.rndRan(0, 100)

	if azurCtrl.upChaName != null and (azurCtrl.upChaName.find("航母") > -1 or azurCtrl.upChaName.find("轻航") > -1) and not azurCtrl.hadUp:
		if rnd < 30:
			azurCtrl.hadUp = true
			return azurCtrl.upCha

	if rnd <= br4:
		br4 = 5
		return "buli3"
	br4 = min(70, br4 + 20)

	if sys.rndPer(65):
		#航母
		return sys.rndListItem(chaData.shipTypeDic[sys.CharaClass.CV])
	else:
		#轻航
		return sys.rndListItem(chaData.shipTypeDic[sys.CharaClass.CVL])